# author: Han Zhang
library(ggplot2)
library(reshape2)
library(showtext)## Add fonts that are available on Windows
showtext.auto(enable = TRUE)
font.add("heiti", "simhei.ttf")

quar <- function(x)
{
  if(x %% 3 == 1) 
    {
    if (x%/%3 == 0)
      {
      return ("Jan")
    }
    if (x%/%3 == 1)
      {return ("Apr")}
    if (x%/%3 == 2)
      {return ("Jul")}
    if (x%/%3 == 3)
      {return ("Oct")}
  }
  else {
    return ("");    
  }
}

plotd <- read.csv("keyword_vs_num_event_700000_fig4.csv", stringsAsFactors = F)
plotd <- plotd[plotd$month >= "2011-01",]
plotd <- plotd[plotd$rank %in% c(10,20,30,40,50),]
plotd$monthd = paste(plotd$month, "-01", sep = "")
plotd$monthd = as.Date(as.character(plotd$monthd), format = "%Y-%m-%d")
plotd$year = strftime(plotd$monthd, format = "%Y")
plotd$quarter = as.numeric(substr(plotd$month,6,8))
plotd$quarter <-   sapply(plotd$quarter,quar)
plotd$month_numeric <- as.numeric(as.factor(plotd$month))


labx = plotd[plotd$rank == 10,]$quarter
cairo_pdf("keyword_vs_num_event_700000.pdf", width = 12, height = 8)

g1 <- ggplot(plotd, aes(x= month_numeric,
                         y = nunique)) + 
  geom_line(aes(linetype = as.character(rank))) +
  scale_color_grey() +
  #geom_bar(stat = "identity") +  
  #geom_smooth() +
  geom_hline(aes(yintercept = 0), linetype = "dashed") +
  theme_bw() + 
     theme(axis.text.y= element_text(size = 14, colour = 'black'),
           axis.title.y=element_text(size = 16, colour = 'black')) +
  theme(axis.text.x= element_text(size = 16, colour = 'black'),
        axis.title.x=element_text(size = 15, colour = 'black')) +
  theme(legend.title = element_text( size=16),
        legend.text = element_text(size = 16))+
  theme(legend.position = c(.9,.8)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())  +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
  ylab ("Count of Collective Action Posts") + 
  xlab ("Month") + 
  guides(linetype = guide_legend(title = "Top-n")) +
  coord_cartesian(ylim = c(-650, 3500), expand = T) + 
  annotate(geom = "text", x = seq_len(length(labx)), y = -300, label = labx, size = 6) +
  annotate(geom = "text", x = 4 + 12 * (0: (length(unique(plotd$year))-1)), y = -600, label = unique(plotd$year), size = 6) 

g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid::grid.draw(g2)
dev.off()

